// component/chatForm/chatForm.ts
import { UrlRequest } from '../../utils/request'

Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    // 表单
    form: {

    },
    formModel: "", // 显示表格
    name: '', //通过二维码的进来
    dockor: "", //通过二维码进来的医生
    teacher: "", //通过二维码进来的患者专员
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 提交表单
    async formSubmit(e: any) {

      // 获取用户列表数据，主要是获取recid
      let data: any = await UrlRequest("https://api.vika.cn/fusion/v1/datasheets/dst2rHLwKUbBvZvAix/records?viewId=viwjBlxEjjf7e&fieldKey=id&pageSize=1000", "GET", "usk2tIP5dJ6rHuomKYhHydU")

      let rest = data.filter((item: any) => item.fields.fldQGM1wHw8gW === wx.getStorageSync('phone'));

      let formData = e.detail.value; // 获取表单数据
      let isEmpty = false; // 标记是否有空内容
      // 检查表单数据是否有空
      for (let key in formData) {
        if (formData[key] === '') {
          isEmpty = true;
          break;
        }
      }

      // 为空提示
      if (isEmpty) {
        wx.showToast({
          title: '请补充完整信息后提交',
          icon: 'none'
        });
      } else {
        this.setData({
          form: formData
        })
        let form: any = this.data.form

        // 将数据给用户资料问卷表
        let records = {
          "records": [
            {
              "recordId": rest[0].fields.fldrRXUVhwq12,
              "fields": {
                "fldMm6aAj2TjL": this.data.name,
                "fldssL0J5xc7p": [this.data.dockor],
                "fldYvxUxxfwbO": [this.data.teacher],
                "fld6pTolc093E": form.identity,
                "fldw8om04HpqA": form.sick,
                "fldOXvh00Y0y0": form.city,
                "fldACynwazSQX": form.gender,
              }
            }
          ],
          "fieldKey": "id"
        }
        await UrlRequest('https://api.vika.cn/fusion/v1/datasheets/dst2rHLwKUbBvZvAix/records?viewId=viwjBlxEjjf7e&fieldKey=id', "PATCH", "usk2tIP5dJ6rHuomKYhHydU", records) as any
        

        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000,
          success: () => {
            this.setData({
              formModel: ""
            })
            this.triggerEvent('getFastGPTAns', '')
          }
        })
      }
    },

    // 拒接提交
    nosubmit() {
      this.setData({
        formModel: ""
      })
    }
  }
})